package com.example.lib_router_compiler.ksp

import com.example.lib_router_anntation.BindView
import com.example.lib_router_anntation.BindView2
import com.example.lib_router_anntation.BindView3
import com.example.lib_router_anntation.RouterPath
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
import com.google.devtools.ksp.validate

/**
 * @author wengcj
 * @date 2023/1/28 17:55
 */
class GenProcessorTest(
    private val codeGenerator: CodeGenerator,
    private val logger: KSPLogger
): SymbolProcessor {
    override fun process(resolver: Resolver): List<KSAnnotated> {
        println("GenProcessor process ")
        logger.warn("GenProcessor process resolver ${resolver.getAllFiles()}")
        resolver.getAllFiles().iterator().forEach {
            logger.warn("GenProcessor process getAllFiles it $it")
        }
        resolver.getNewFiles().iterator().forEach {
            logger.warn("GenProcessor process getNewFiles it $it")
        }
        val symbols = resolver.getSymbolsWithAnnotation(BindView::class.qualifiedName!!)
        val symbols1 = resolver.getSymbolsWithAnnotation(BindView2::class.qualifiedName!!)
        val symbols2 = resolver.getSymbolsWithAnnotation(BindView3::class.qualifiedName!!)
        val RouterPath = resolver.getSymbolsWithAnnotation(RouterPath::class.qualifiedName!!)
        logger.warn("GenProcessor process symbols $symbols symbols1 $symbols1 symbols2 $symbols2")
        var ret = symbols.filter { !it.validate() }.toMutableList()
        val butterKnifeList = symbols
            .filter { it is KSPropertyDeclaration && it.validate() }
            .map {
                it as KSPropertyDeclaration
            }.toList()
        val butterKnifeList1 = symbols1
            .filter { it is KSPropertyDeclaration && it.validate() }
            .map {
                it as KSPropertyDeclaration
            }.toList()
        //KSFunctionDeclaration 查找注解对应的成员变量明
        val butterKnifeList2 = symbols2
            .filter { it is KSPropertyDeclaration  }
            .map {
                it as KSPropertyDeclaration
            }.toList()
        //KSClassDeclaration 查找注解对应的class类
        val butterKnifeList3 = RouterPath
            .filter { it is KSClassDeclaration }
            .map {
                it as KSClassDeclaration
            }.toList()
        //KSFunctionDeclaration 查找注解对应的方法
        val butterKnifeList4 = RouterPath
            .filter { it is KSFunctionDeclaration && it.validate() }
            .map {
                it as KSFunctionDeclaration
            }.toList()
        ButterKnifeGenerator().generate(codeGenerator, logger, butterKnifeList)


        logger.warn("GenProcessor process butterKnifeList $butterKnifeList ret $ret")
        logger.warn("GenProcessor process butterKnifeList1 $butterKnifeList1")
        logger.warn("GenProcessor process butterKnifeList2 $butterKnifeList2")
        logger.warn("GenProcessor process butterKnifeList3 $butterKnifeList3")
        logger.warn("GenProcessor process butterKnifeList4 $butterKnifeList4")
        //测试第二次返回内容
//        ret.addAll(butterKnifeList3)
//        ret.addAll(butterKnifeList4)
        logger.warn("ret $ret")
        //process app模块会执行两次 第二次的内容为第一次process返回的内容 
        return ret
    }
}